package com.yuanfengPlus.basicInfoManage.paymode.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yuanfengPlus.basicInfoManage.paymode.dao.IPayModeDustbinMapper;
import com.yuanfengPlus.basicInfoManage.paymode.dao.IPayModeMapper;
import com.yuanfengPlus.basicInfoManage.paymode.mouel.PayModeDustbinInfo;
import com.yuanfengPlus.basicInfoManage.paymode.mouel.PayModeInfo;
import com.yuanfengPlus.basicInfoManage.paymode.service.IPayModeService;
import com.yuanfengPlus.utils.PageResult;
import com.yuanfengPlus.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class PayModeServiceImpl implements IPayModeService {
    @Resource
    private IPayModeMapper ipmm;
    @Resource
    private IPayModeDustbinMapper ipmdm;
    private PayModeDustbinInfo payModeDustbinInfo = new PayModeDustbinInfo();
    @Override
    public Result<String> insert(PayModeInfo payMethodInfo) {
        if (payMethodInfo.getPayment_methods_name() == null)
            return Result.failure(500, "支付方式名称不能为空");
        Integer insert = ipmm.insert(payMethodInfo);
        return insert > 0 ? Result.success("添加成功") : Result.failure(500, "添加失败");
    }

    @Override
    public Result<String> update(PayModeInfo payMethodInfo) {
        Integer update = ipmm.update(payMethodInfo);
        return update > 0 ? Result.success("修改成功") : Result.failure(500, "修改失败");
    }

    @Override
    public PageResult select(Map<String, Object> params) {
        PageHelper.startPage((int) params.get("pageNum"), (int) params.get("pageSize"));
        Page<PayModeInfo> page = ipmm.select(params);
        return new PageResult(page.getTotal(), page.getResult());
    }



    // 软删除
    @Override
    public Result<String> delete(String payMethodId) {
        if (payMethodId == null) return Result.failure(500, "支付方式ID不能为空");
        PayModeInfo select = ipmm.selectById(payMethodId);
        select.setDeleted(1);
        Integer update = ipmm.update(select);
        if (update <= 0) return Result.failure(500, "删除失败");
        payModeDustbinInfo.setPayment_methods_ID(select.getPayment_methods_ID());
        payModeDustbinInfo.setPayment_methods_name(select.getPayment_methods_name());
        payModeDustbinInfo.setPayment_methods_remark(select.getPayment_methods_remark());
        Integer insert = ipmdm.insert(payModeDustbinInfo);
        if (insert <= 0) return Result.failure(500, "删除失败");
        return Result.success("删除成功");
    }

    @Override
    public Result<List<PayModeDustbinInfo>> selectAllDustbin() {
        List<PayModeDustbinInfo> select = ipmdm.select();
        if (select == null && select.isEmpty())
            return Result.failure(500, "回收站为空");
        return Result.success(select);
    }
}
